﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PASNew.U.Controls
{
    public partial class Login : System.Web.UI.UserControl
    {
		
        protected void Page_Load(object sender, EventArgs e)
        {
			
			if (Session["UserIdentity"] != null)
			{
				UserDetailsDto user = (UserDetailsDto)Session["UserIdentity"];
				pnlLogin.Visible = false;
				pnlLoggedIn.Visible = true;
				lblLogingName.Text = user.FirstName;
			}
			else
			{
				pnlLogin.Visible = true;
				pnlLoggedIn.Visible = false;
			}
        }
        protected void LBLogin_Click(object sender, EventArgs e)
        {
            lblUnsuccessfull.Visible = false;

            string useremail = txtBoxEmail.Text.ToString();
            string password = txtBoxPassword.Text.ToString();


			UserDetailsDto user = new UserDetailsDto
								   {
									   UserID = 1,
									   FirstName = "Test User",
								   };

			Session["UserIdentity"] = user;

			pnlLogin.Visible = false;
			pnlLoggedIn.Visible = true;
			lblLogingName.Text = user.FirstName;

			Response.Redirect("/pages/notifications.aspx");
			//using (PASDataDataContext data = new PASDataDataContext())
			//{
			//    var usercheck = (from u in data.UserDetails
			//                     where u.UserEmail == useremail
			//                     && u.UserPassword == password
			//                     select u).FirstOrDefault();

			//    if (usercheck != null)
			//    {
			//        var compident = (from cid in data.CompanyUserRelations
			//                         where cid.UserId == usercheck.UserId
			//                         select cid).FirstOrDefault();

			//        if (usercheck != null && compident != null)
			//        {
			//            UserDetailsDto user = new UserDetailsDto
			//                       {
			//                           UserID = usercheck.UserId,
			//                           FirstName = usercheck.UserName,
			//                       };

			//            Session["UserIdentity"] = user;

			//            CompIDdto compIdentity = new CompIDdto
			//            {
			//                CompID = compident.CompanyId,
			//            };

			//            Session["CompanyIdentitifier"] = compIdentity;

			//            if (usercheck.UserLevel == 1)
			//            {
			//                Response.Redirect("/pages/Backend.aspx");
			//            }
			//            else
			//            {
			//                Response.Redirect("/pages/Notifications.aspx");
			//            }
			//        }
			//    }
			//    else
			//    {
			//        ctlError.Visible = true;
			//    }
			//}
        }

		protected void btnLogout_Click(object sender, EventArgs e)
		{
				Session.Clear();
				Response.Redirect("/pages/home.aspx");
		}
    }
}